﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Configuration;

using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

namespace ReaderWithConfig
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Data Readers *****\n");

            


            #region Connection object code

            // Create and open a connection.
            
            string provider = ConfigurationManager.ConnectionStrings["ReaderWithConfig.Properties.Settings.Database1ConnectionString"].ProviderName;
            string connStr = ConfigurationManager.ConnectionStrings["ReaderWithConfig.Properties.Settings.Database1ConnectionString"].ConnectionString;

            DbProviderFactory dbFactory = DbProviderFactories.GetFactory(provider);

            DbConnection cn = dbFactory.CreateConnection();
            cn.ConnectionString = connStr;
            cn.Open();
            ShowConnectionStatus(cn);
            
            #endregion
            Person emp = new Employee();
            
            
            

            // Create a SQL command object.
            string strSQL = @"insert into Persons(PhoneNumber, Name, Email, Password, Persontype, LoginId)
                values ('25658599', 'hans', 'hans@hans.dk', 1234, 'Employe', 'hans')";
            string sql = "SELECT * FROM Persons";
            //connStr.Insert(1, emp.getSetPhoneNumber.ToString());
            //connStr.Insert(2, emp.getSetName);
            //connStr.Insert(3, emp.getSetEmail);
            //connStr.Insert(4, emp.getSetPassword);
            //connStr.Insert(5, emp.GetType().ToString());
            //connStr.Insert(6, emp.getLoginID());
            
            
            
            
            //string strSQL = "SELECT * FROM Employee";
            //string strSQL = "SELECT * FROM Client";
            DbCommand myCommand = dbFactory.CreateCommand();

            myCommand.CommandText = sql;
            myCommand.Connection = cn;

            #region Data reader code
            // Obtain a data reader ala ExecuteReader().
            DbDataReader myDataReader;
            myDataReader = myCommand.ExecuteReader();
            // CommandBehavior.CloseConnection);
            // myDataReader.Close();
            // Loop over the results.
            while (myDataReader.Read())//while(more rows)
            {
                for (int i = 0; i < myDataReader.FieldCount; i++)//Display each attribute in current row
                {
                    Console.Write("{0} = {1} ",
                        myDataReader.GetName(i),
                        myDataReader.GetValue(i).ToString().Trim());
                    // Console.Write(myDataReader.GetName(i)+" = "+myDataReader.GetValue(i).ToString().Trim());
    
                }
                Console.WriteLine();
                Console.ReadLine();
            }
            
            #endregion

            Console.WriteLine();
            Console.Write("Close connections?"); Console.ReadLine();
            Console.WriteLine();

            myDataReader.Close();
            // If we specify CommandBehavior.CloseConnection, we 
            // don't need to explicitly call Close() on the connection.
            cn.Close();
            ShowConnectionStatus(cn);
            Console.ReadLine();
        }

        private static void ShowConnectionStatus(DbConnection cn)
        {
            // Show various stats about current connection object.
            Console.WriteLine("***** Info about your connection *****");
            Console.WriteLine("Database location: {0}", cn.DataSource);
            Console.WriteLine("Database name: {0}", cn.Database);
            Console.WriteLine("Timeout: {0}", cn.ConnectionTimeout);
            Console.WriteLine("Connection state: {0}\n", cn.State.ToString());
        }
    }
}
